package com.example.springapplicationdemo.exceptionhandler;

import com.example.springapplicationdemo.exceptionhandler.exception.UserException;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@Order(-10000)
@ControllerAdvice(annotations = Controller.class)// 指定Advice应用于Controller注解
public class UserControllerAdvice {

    /**
     * ControllerAdvice中的ExceptionHandler会对ControllerAdvice作用域中的Controller生效
     * @param e
     * @return
     */
    // 不指定待处理的异常类型，所有UserException的子类型都会被处理
    @ExceptionHandler
    public ModelAndView handleUserException(UserException e){
        ModelAndView mv =  new ModelAndView("error");
        mv.addObject("error", e);
        return mv;
    }
}
